package com.hunk.utils;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.Properties;


/**
 * <Description> 
 * Property使用库 
 * </Description>
 * <Files>
 * 	PropertyHelper.java
 *  propertyHelper.properties
 * </Files>
 * @author Hunk
 * @since 2013-1-11
 */
public class PropertyHelper {	
	/** property variable **/
	private final Properties props = new Properties();
	private final static String filePath = "propertyHelper.properties";
	
	private PropertyHelper() {}
	private static PropertyHelper instance = null;
	/**
	 * 获取系统属性实例
	 * @return
	 */
	public static PropertyHelper getInstance() {
		if (instance == null) {
			instance = new PropertyHelper();
			instance.load();
		}
		return instance;
	}
	
	/**
	 * 获取系统属性实例
	 * @return
	 */
	public static PropertyHelper getInstance(String filePath) {
		if (instance == null) {
			instance = new PropertyHelper();
			instance.load(filePath);
		}
		return instance;
	}
	
	public void load() {
		String _filePath = this.getClass().getClassLoader().getResource(filePath).getPath();
		load(_filePath);
	}
	
	public void write(String parameterName,String parameterValue) throws IOException {
		String _filePath = this.getClass().getClassLoader().getResource(filePath).getPath();
		write(_filePath,parameterName,parameterValue);
	}
	
	public Properties getProperties() {
		return props;
	}
	
	/** Test Main
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		URL url = PropertyHelper.class.getClassLoader().getResource(
				"PorpertyHelper.properties");
		String filePath = url.getPath();
		PropertyHelper ph = new PropertyHelper();
		ph.load(filePath);
		ph.write(filePath, "ss", "og");
		Print.println("---------");		
	}

	/** load properties in variable from file
	 * @param filePath
	 */
	public void load(String filePath) {
		InputStream in = null;
		try {
			in = new BufferedInputStream(
										new FileInputStream(filePath)
										);
			props.load(in);
		} catch (Exception e) {
			e.printStackTrace();
			Print.error("load\"" + filePath + "\"failed.");
		} finally {
			try {
				if (in != null) in.close();
			} catch (IOException e) {
				// TODO printStackTrace
				e.printStackTrace();
			}
		}
	}

	/** Write Property In File
	 * @param filePath
	 * @param parameterName
	 * @param parameterValue
	 * @throws IOException
	 */
	public void write(String filePath, String parameterName,
			String parameterValue) throws IOException {
		OutputStream fos = null;
		try {
//			InputStream fis = new FileInputStream(filePath);
			// 从输入流中读取属性列表（键和元素对）
//			_props.load(fis);
			// 调用 Hashtable 的方法 put。使用 getProperty 方法提供并行性,强制要求为属性的键和值使用字符串。返回值是
			// Hashtable 调用 put 的结果。
			fos = new FileOutputStream(filePath);
			props.setProperty(parameterName, parameterValue);
			// 以适合使用 load 方法加载到 Properties 表中的格式，将此 Properties
			// 表中的属性列表（键和元素对）写入输出流
			props.store(fos, "Update '" + parameterName + "' value");
		} catch (IOException e) {
			Print.error("Visit " + filePath + " for updating " + parameterName
					+ " value error");
			throw e;
		} finally {
			try {
				if (fos != null)
					fos.close();
			} catch (IOException e) {
				// TODO printStackTrace 
				e.printStackTrace();
			}
		}
	}
	
	/**getString
	 * @param key
	 * @return
	 */
	public String getString(String key) {
		return props.getProperty(key);
	}
	
	/**getInt
	 * @param key
	 * @return
	 */
	public int getInt(String key) {
		return Integer.parseInt(getString(key));
	}
	
	/**getFloat
	 * @param key
	 * @return
	 */
	public float getFloat(String key) {
		return Float.parseFloat(getString(key));
	}
	
	/**getDouble
	 * @param key
	 * @return
	 */
	public double getDouble(String key) {
		return Double.parseDouble(getString(key));
	}
}
